#include "default_task.h"


void default_task(void const * argument){
	default_task_init();
	while(1){
		switch(core_step){
			case CORE_PARAM_WAIT_INIT:
				break;
			case CORE_RUN:
				core_run_func();
				break;
			default:
				break;
		}
		EXECUTE_ALL_FUNCS(NULL);
		osDelay(1);
	}
}

static void default_task_init(void){
	HAL_GPIO_WritePin(Led_Port_Cast(LED_Left), Led_Pin_Cast(LED_Left), led_config.led_left_init_state?GPIO_PIN_SET:GPIO_PIN_RESET);
	HAL_GPIO_WritePin(Led_Port_Cast(LED_Right), Led_Pin_Cast(LED_Right), led_config.led_right_init_state?GPIO_PIN_SET:GPIO_PIN_RESET);
 	led_config.led_left_toggle_time = 1000/led_config.led_left_freq;
	led_config.led_right_toggle_time = 1000/led_config.led_right_freq;
}

void set_led_tog_time(){
	led_config.led_left_toggle_time = 1000 / led_config.led_left_freq;
	led_config.led_right_toggle_time = 1000 / led_config.led_right_freq;
}

static void core_run_func(void){
	set_led_tog_time();
	switch(led_config.led_left_state){
		case LED_BRIGHT:
			HAL_GPIO_WritePin(Led_Port_Cast(LED_Left), Led_Pin_Cast(LED_Left), GPIO_PIN_SET);
			break;
		case LED_EXTINGUISH:
			HAL_GPIO_WritePin(Led_Port_Cast(LED_Left), Led_Pin_Cast(LED_Left), GPIO_PIN_RESET);
			break;
		case LED_FLASHING:
			if(Main_time_ms_cnt%led_config.led_left_toggle_time == 0){
				HAL_GPIO_TogglePin(Led_Port_Cast(LED_Left), Led_Pin_Cast(LED_Left));
			}
			break;
		default:
			break;
	}
	
	switch(led_config.led_right_state){
		case LED_BRIGHT:
			HAL_GPIO_WritePin(Led_Port_Cast(LED_Right), Led_Pin_Cast(LED_Right), GPIO_PIN_SET);
			break;
		case LED_EXTINGUISH:
			HAL_GPIO_WritePin(Led_Port_Cast(LED_Right), Led_Pin_Cast(LED_Right), GPIO_PIN_RESET);
			break;
		case LED_FLASHING:
			if(Main_time_ms_cnt%led_config.led_right_toggle_time == 0){
				HAL_GPIO_TogglePin(Led_Port_Cast(LED_Right), Led_Pin_Cast(LED_Right));
			}
			break;
		default:
			break;
	}
	
}
